import { isString, isArray } from "lodash-es";

const SIGN = "#_e_#";

export const fieldEncode = (key, field) => {
  if (isString(field)) {
    return key + SIGN + field;
  }
  if (isArray(field)) {
    return key + SIGN + JSON.stringify(field);
  }
};

export const fieldDecode = str => {
  if (!str) return;
  if (!isString(str)) return str;
  const value = str.split(SIGN);
  if (
    value[1]?.length &&
    value[1][0] === "[" &&
    value[1][value[1].length - 1] === "]"
  ) {
    return {
      key: value[0],
      value: JSON.parse(value[1])
    };
  }
  return {
    key: value[0],
    value: value[1]
  };
};
